package com.modifiertech.lineage.commands;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.LineageRegistries;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.api.PlayerDataSkill;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.handler.SkillLevelUpHandler;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CmdSkillModify extends CommandBase {
    public static final String[] SKILL_OPTS_STR=new String[]{
            BaseConst.PLAYER_ADV_TAG__SKILLS_LEARN,
            BaseConst.PLAYER_ADV_TAG__SKILLS_FORGET,
            BaseConst.PLAYER_ADV_TAG__SKILLS_FORGET_ALL,
            BaseConst.PLAYER_ADV_TAG__SKILLS_TOGGLE};
    public static final List<String> SKILL_OPTS=new ArrayList<>(Arrays.asList(SKILL_OPTS_STR));

    @Override
    public String getName() {
        return "lineageskill";
    }

    @Override
    public String getUsage(ICommandSender iCommandSender) {
        return "lineage.cmdskillmodify.useage";
    }

    //                       0       1                2
    //lineageskill  @p awadakada learn
    @Override
    public void execute(MinecraftServer minecraftServer, ICommandSender iCommandSender, String[] strings) throws CommandException {
        if (strings.length == 0) {
            throw new CommandException("lineage.cmdskillmodify.invalid.missing.playername");
        }
        if (strings.length == 1) {
            throw new CommandException("lineage.cmdskillmodify.invalid.missing.skilltype");
        }
        if(strings.length==2){
            throw new CommandException("lineage.cmdskillmodify.invalid.missing.opttype");
        }
        boolean ifSilent=CmdHelper.ifExecAsSilent(strings);
        if(ifSilent)strings=CmdHelper.cutSilentCmd(strings);
        EntityPlayerMP entityPlayerMP=strings.length>1 ? CommandBase.getPlayer(minecraftServer,iCommandSender,strings[0])
                :CommandBase.getCommandSenderAsPlayer(iCommandSender);
        //skilltype
        ResourceLocation skillname=new ResourceLocation(BaseConst.MODID,strings[1]);
        if(!LineageRegistries.SKILLS.containsKey(skillname)){
            throw new CommandException("lineage.cmdskillmodify.invalid.skilltype",skillname);
        }
        if(!SKILL_OPTS.contains(strings[2])){
            throw new CommandException("lineage.cmdskillmodify.invalid.opttype",skillname);
        }
        PlayerData playerData= PlayerDataHandler.get(entityPlayerMP);
        //遗忘所有
        if (BaseConst.PLAYER_ADV_TAG__SKILLS_FORGET_ALL.equals(strings[2])){
            SkillLevelUpHandler.INSTANCE.skillForgetAllCmdUse(entityPlayerMP,playerData);
        //学习对应技能
        }if(BaseConst.PLAYER_ADV_TAG__SKILLS_LEARN.equals(strings[2])){
            SkillLevelUpHandler.INSTANCE.skillLevelUpCmdUse(entityPlayerMP,strings[1],1,playerData);
        //其他
        }else{
            playerData.optSkill(strings[1],strings[2]);
        }
//        CmdHelper.execServerSay(new TextComponentTranslation("lineage.cmd.cmdskillmodify.msg",strings[0],strings[1],strings[2]));
    }



    @Nonnull
    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
        if (args.length == 1) {
            return Arrays.asList(server.getPlayerList().getOnlinePlayerNames());
        }
        if (args.length == 2) {
            String skill = args[1];
            return LineageRegistries.SKILLS.getValuesCollection().stream().map(PlayerDataSkill::getSkillName).filter(
                    skillName -> skillName.startsWith(skill)).collect(Collectors.toList()
            );
        }
        if (args.length == 3) {
            String optname = args[2];
            return Arrays.stream(SKILL_OPTS_STR).filter(name -> name.startsWith(optname)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

}
